#include "editpatientinfo.h"
#include "messagetype.h"
#include <QMessageBox>
#include <QFormLayout>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPalette>
#include <QColor>
#include <QLabel>

EditPatientInfo::EditPatientInfo(QTcpSocket *socket, QWidget *parent)
    : QWidget(parent), tcpSocket(socket) {
    setWindowTitle("编辑个人信息");
    setFixedSize(600, 600); // 增大窗口大小以提供更多空间

    QPalette palette = this->palette();
    palette.setColor(QPalette::Window, QColor(248, 248, 255));
    this->setPalette(palette);
    this->setAutoFillBackground(true);

    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(20, 20, 20, 20); // 添加外边距
    mainLayout->setSpacing(15);

    QFormLayout *formLayout = new QFormLayout();
    formLayout->setSpacing(15);

    // Create input fields with styles
    idEdit = new QLineEdit(this);
    idEdit->setPlaceholderText("请输入患者ID");
    setInputFieldStyle(idEdit);

    nameEdit = new QLineEdit(this);
    nameEdit->setPlaceholderText("请输入姓名");
    setInputFieldStyle(nameEdit);

    sexEdit = new QLineEdit(this);
    sexEdit->setPlaceholderText("请输入性别");
    setInputFieldStyle(sexEdit);

    ageEdit = new QLineEdit(this);
    ageEdit->setPlaceholderText("请输入年龄");
    setInputFieldStyle(ageEdit);

    phoneEdit = new QLineEdit(this);
    phoneEdit->setPlaceholderText("请输入电话号码");
    setInputFieldStyle(phoneEdit);

    addressEdit = new QLineEdit(this);
    addressEdit->setPlaceholderText("请输入地址");
    setInputFieldStyle(addressEdit);

    passwardEdit = new QLineEdit(this);
    passwardEdit->setEchoMode(QLineEdit::Password);
    passwardEdit->setPlaceholderText("请输入密码");
    setInputFieldStyle(passwardEdit);

    formLayout->addRow("患者ID", idEdit);
    formLayout->addRow("姓名", nameEdit);
    formLayout->addRow("性别", sexEdit);
    formLayout->addRow("年龄", ageEdit);
    formLayout->addRow("电话", phoneEdit);
    formLayout->addRow("地址", addressEdit);
    formLayout->addRow("密码", passwardEdit);

    mainLayout->addLayout(formLayout);

    // Button layout with adjustable spacings
    QHBoxLayout *buttonLayout = new QHBoxLayout();
    QPushButton *saveButton = createStyledButton("保存", "#4CAF50");
    QPushButton *backButton = createStyledButton("返回", "#f44336");

    buttonLayout->addStretch(); // Add stretchable space before buttons
    buttonLayout->addWidget(saveButton);
    buttonLayout->addWidget(backButton);
    buttonLayout->addStretch(); // Add stretchable space after buttons

    mainLayout->addSpacing(20);
    mainLayout->addLayout(buttonLayout);
    setLayout(mainLayout);

    connect(saveButton, &QPushButton::clicked, this, &EditPatientInfo::onSaveButtonClicked);
    connect(backButton, &QPushButton::clicked, this, &EditPatientInfo::onBackButtonClicked);
}

// Function to style input fields
void EditPatientInfo::setInputFieldStyle(QLineEdit *lineEdit) {
    lineEdit->setStyleSheet("padding: 8px; border: 1px solid #ccc; border-radius: 5px;");
    lineEdit->setFixedHeight(35);
}

// Helper function to create styled buttons
QPushButton* EditPatientInfo::createStyledButton(const QString &text, const QString &color) {
    QPushButton *button = new QPushButton(text, this);
    button->setStyleSheet(QString("background-color: %1; color: white; "
                                  "border: none; border-radius: 5px; padding: 10px;").arg(color));
    button->setFixedHeight(40);
    return button;
}

void EditPatientInfo::onSaveButtonClicked() {
    if (idEdit->text().isEmpty()) {
        QMessageBox::warning(this, "输入错误", "患者ID是必填项，请填写");
        return;
    }

    QMap<QString, QString> updateData;
    updateData.insert("type", PATIENTUSRDATABASE);
    updateData.insert("databaseoperator", QString::number(UPDATE));
    updateData.insert("id", idEdit->text());
    updateData.insert("name", nameEdit->text());
    updateData.insert("sex", sexEdit->text());
    updateData.insert("age", ageEdit->text());
    updateData.insert("phone", phoneEdit->text());
    updateData.insert("address", addressEdit->text());
    updateData.insert("passward", passwardEdit->text());
    QByteArray updateRequest = mapToByteArrayCustom(updateData);

    if (tcpSocket->state() == QAbstractSocket::UnconnectedState) {
        tcpSocket->connectToHost("127.0.0.1", 12345);
    }
    if (tcpSocket->waitForConnected(5000)) {
        tcpSocket->write(updateRequest);
        tcpSocket->flush();
    }
}

void EditPatientInfo::onBackButtonClicked() {
    this->close();
}
